<?php
class ControllerDelimanLichsudatbaocuakhachhang extends Controller
{
	private $error = array();
   	function __construct() 
	{
		$listchek = $this->getListCheck();
		if(in_array($this->getMethod(),$listchek))
		{
			if(!$this->user->hasPermission($this->getRoute(), $this->getMethod()))
			{
				$this->response->redirect("?route=common/permission");
			}
		}
		
	 	$this->load->model("deliman/khachhang");
		$this->load->model('deliman/nhanvien');
		$this->load->model('deliman/loaibao');
		$this->load->model('deliman/khuvuc');
		$this->load->model("common/control");
   	}
	
	public function index()
	{
		$this->getList();
	}
		
	private function getList() 
	{		
		$data = $this->request->get;
		$where = "";
		
		$this->data['dskhuvuc']=array();
		//$this->data['dskybao']=array();
		$rows=array();
		//$this->data["datas"][] = array();
		$arr_khuvucid = array();
		
		// Nếu là QLKVP: Lay ds khuvucid của các khu vực mà nhân viên đang đăng nhập quản lý	
		if(trim($this->user->getNhanVien('loainhanvienid')) == '2')
		{
			$data_khuvuc = $this->model_deliman_khuvuc->getList(" AND nhanvienphutrachid ='".$this->user->getNhanVien('nhanvienid')."'");
			$arr_khuvucid = $this->string->matrixToArray($data_khuvuc, 'khuvucid');
			$arr_khuvucid = array_unique($arr_khuvucid);			
		}
		else
		{
			// Nếu là NV QL chung hoặc NV QTHT: cho phép xem hết
			$arr_khuvucid[]='0';
		}
		
		//Danh sach khu vuc	theo TREE
		foreach($arr_khuvucid as $khuvucid)
		{
			$dskv=array();
			$this->model_deliman_khuvuc->getTree($khuvucid,$dskv,$khuvucid);
			$this->data['dskhuvuc'] = array_merge($this->data['dskhuvuc'], $dskv);
		}
		
		// MUON XEM THEO KHACH HANG
		
		/*if($data['khachhangid']!="")
		{
			if($data['sodienthoai']=="")
				$rows[] = $this->getData($data['khachhangid']);
			else
			{
				$wherekhachhang = " AND dienthoai like '%". $data['sodienthoai']."%' AND khachhangid = '".$data['khachhangid']."'";	
				$data_khachhang = $this->model_deliman_khachhang->getList($wherekhachhang);
				foreach($data_khachhang as $khachhang)
					$rows[] = $this->getData($khachhang['khachhangid']);
			}
		}
		else  /////////////////////// MUON XEM THEO KHU VUC
		{					
			// Nhan vien co chon KHU VUC muon xem
			if($data['khuvucid']!="")
			{
				
				if(trim($data['khuvucid'])==trim('all'))
				{
					
					//Khong chon KHU VUC thi Su dung: $this->data['dskhuvuc'] => XEM HET NHUNG KHU VUC QL
					$data_khuvuc = $this->data['dskhuvuc'];	
					
				}
				else
				{
					$data_khuvuc = array();
					$this->model_deliman_khuvuc->getTree($data['khuvucid'],$data_khuvuc);
				}
			}
			else
			{
				//$dskhuvucid = $this->data['dskhuvuc'];	
				// Mặc định: Không cho xem 
				$data_khuvuc[] = '';							
			}
			
			$arr_khuvucid = array();
			$arr_khuvucid = $this->string->matrixToArray($data_khuvuc,'khuvucid');
			$arr_khuvucid = array_unique($arr_khuvucid);
			
			//Lay ds nhung KHACH HANG trong khu vuc do
			$where = " AND khuvucid in( '".implode("','",$arr_khuvucid)."')";
			$data_khachhang = $this->model_deliman_khachhang->getList(" AND khuvucid in( '".implode("','",$arr_khuvucid)."')");
			$arr_khachhangid = $this->string->matrixToArray($data_khachhang,'khachhangid');
			$arr_khachhangid = array_unique($arr_khachhangid);
			
			foreach($arr_khachhangid as $khachhangid )
			{										
				$rows[] = $this->getData($khachhangid);				
			}
		}*/
		$wherekhachhang = "";
		if($data['sodienthoai']!="")				
		{
			$wherekhachhang .= " AND dienthoai = '". $data['sodienthoai']."'";	
		}
		if($data['khuvucid'] !="")
		{
			$data_khuvuc = array();
			$this->model_deliman_khuvuc->getTree($data['khuvucid'],$data_khuvuc);
			$arr_khuvucid = array();
			$arr_khuvucid = $this->string->matrixToArray($data_khuvuc,'khuvucid');
			$arr_khuvucid = array_unique($arr_khuvucid);
			
			$wherekhachhang .= " AND khuvucid in('". implode("','",$arr_khuvucid) ."')";	
		}
		else
		{
			$arr_khuvucid = $this->string->matrixToArray($this->data['dskhuvuc'],'khuvucid');
			$wherekhachhang .= " AND khuvucid in('". implode("','",$arr_khuvucid) ."')";
		}
		if($data['khachhangid'] !="")
		{
			$wherekhachhang .= " AND khachhangid = '". $data['khachhangid']."'";	
		}
		$data_khachhang = $this->model_deliman_khachhang->getList($wherekhachhang);
		foreach($data_khachhang as $khachhang)
			$rows[] = $this->getData($khachhang['khachhangid']);
			
		//Duyệt lấy datas
		foreach($rows as $key=> $item)
		{
			if(count($item['dskybao']))
				$this->data['datas'][$key] = $item;
		}
		$this->data['refres']=$_SERVER['QUERY_STRING'];
		$this->id='content';
		$this->template="deliman/lichsudatbaocuakhachhang.tpl";
		if($data['print'] == "true")
			$this->layout="layout/print";
		else
			$this->layout="layout/deliman";
		
		$this->render();
	}
	
	private function getData($khachhangid)
	{
		//Danh sach ky bao ma khach hang da dat
		$dskybao = array();
		$where = " AND khachhangid = '".$khachhangid."' ORDER BY ngayphathanh DESC";		
		$dskybao = $this->model_deliman_khachhang->getThongTinDatBaoList($where);
		//Lay loai bao
		$dsloaibaoid = array();
		$dsloaibaoid = $this->string->matrixToArray($dskybao, 'loaibaoid');
		$dsloaibaoid = array_unique($dsloaibaoid);
		$dsloaibao = array();
		$dsloaibao = $this->model_deliman_loaibao->getList(" AND loaibaoid in( '".implode("','",$dsloaibaoid)."')");
					
		//Thong tin ve khach hang dang xem	
		$khachhang = array();	
		$khachhang = $this->model_deliman_khachhang->getItem($khachhangid);
		$data = array(										
							'dsloaibao' =>$dsloaibao,
							'dskybao'=>$dskybao,
							'khachhang'=>$khachhang											
						);
		return $data;
	}
	
	// Lay ds KHACH HANG trong khu vuc duoc chon
	public function getKhachHang()
	{	
		$khuvucid = $this->request->get['khuvucid'];
		$select =  $this->request->get['select'];
		
		$khuvus = array();
		
		if($khuvucid=="")
		{
			$arr_khuvucid = array();
		
			// Nếu là QLKVP: Lay ds khuvucid của các khu vực mà nhân viên đang đăng nhập quản lý	
			if(trim($this->user->getNhanVien('loainhanvienid')) == '2')
			{
				$data_khuvuc = $this->model_deliman_khuvuc->getList(" AND nhanvienphutrachid ='".$this->user->getNhanVien('nhanvienid')."'");
				$arr_khuvucid = $this->string->matrixToArray($data_khuvuc, 'khuvucid');
				$arr_khuvucid = array_unique($arr_khuvucid);			
			}
			else
			{
				// Nếu là NV QL chung hoặc NV QTHT: cho phép xem hết
				$arr_khuvucid[]='0';
			}
			
			//Danh sach khu vuc	theo TREE
			$data_khuvucid=array();
			foreach($arr_khuvucid as $khuvucid)
			{
				$dskv=array();
				$this->model_deliman_khuvuc->getTree($khuvucid,$dskv,$khuvucid);
				$data_khuvucid = array_merge($data_khuvucid, $dskv);
			}
			$arrkhuvucid = $this->string->matrixToArray($data_khuvucid,'khuvucid');
			$cmbDanhSachKhachHang = $this->model_deliman_khachhang->getList(" AND khuvucid in( '".implode("','",$arrkhuvucid)."')");
		}
		else
		{
			$this->model_deliman_khuvuc->getTree($khuvucid, $khuvus);		
			$arrkhuvucid = $this->string->matrixToArray($khuvus,'khuvucid');
			$cmbDanhSachKhachHang = $this->model_deliman_khachhang->getList(" AND khuvucid in( '".implode("','",$arrkhuvucid)."')");
		}
		
		$this->data['output'] = '<option value="">Chọn tất cả</option>'.$this->model_common_control->getDataCombobox($cmbDanhSachKhachHang, "hoten", "khachhangid", $select);
		
		$this->template='common/output.tpl';
		$this->render();
	}
	
}
?>